package com.google.code.chaotic.server;

import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import com.google.appengine.api.datastore.KeyFactory;
import com.google.code.chaotic.rpc.JavaSvc;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class JavaSvcImpl extends RemoteServiceServlet //
		implements JavaSvc {

	@Override
	public Map<String, String> getCountries(String sessionId) {
		SortedMap<String, String> map = new TreeMap<String, String>();
		for (Locale locale : Locale.getAvailableLocales()) {
			String code = locale.getCountry();
			if (!map.containsKey(code))
				map.put(locale.getDisplayCountry(), code);
		}
		// Workaround for the omission of Iran (due to sanctions!)
		map.put("Iran", "IR");
		// Collections.sort(list);
		return map;
	}

	@Override
	public Long decodeKey(String encodedKey) {
		return KeyFactory.stringToKey(encodedKey).getId();
	}
}
